package com.lib76.socket.impl;

import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.DatagramChannelFactory;
import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;

import com.lib76.socket.SocketObject;
import com.lib76.socket.model.SocketInfo;

public class ByteUdpSocketObject extends SocketObject{

	public ByteUdpSocketObject(SocketInfo info) {
		super(info);
	}

	@Override
	protected Channel runServiceSocket() throws Exception {
		DatagramChannelFactory udpChannelFactory = new NioDatagramChannelFactory(Executors.newCachedThreadPool()); 
		ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(udpChannelFactory);
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() {
                return Channels.pipeline(new Handler());
            }
        });
		
		Channel channel = bootstrap.bind(info.getLocalSocketAddress());
		return channel;
	}

	@Override
	protected ClientBootstrap runTcpClientSocket() throws Exception {
		return null;
	}

	@Override
	protected ConnectionlessBootstrap runUdpClientSocket() throws Exception {
		DatagramChannelFactory udpChannelFactory = new NioDatagramChannelFactory(Executors.newCachedThreadPool()); 
		ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(udpChannelFactory);
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() {
                return Channels.pipeline(new Handler());
            }
        });
		return bootstrap;
	}

}
